package com.example.demo.controller;

import com.example.demo.entity.R;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import com.example.demo.util.JwtUtils;
import com.example.demo.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/user/list")
//    @PreAuthorize("hasRole('ROLE_admin1')")
    @PreAuthorize("hasAuthority('system:user1:list')")
    public R userList(@RequestHeader(required = false) String token) {
        if (StringUtil.isNotEmpty(token)) {
            Map<String, Object> resutMap = new HashMap<>();
            List<SysUser> userList = sysUserService.list();
            resutMap.put("userList", userList);
            return R.ok(resutMap);
        } else {
            return R.error(401, "没有权限访问");
        }
    }

    @GetMapping("/login")
    public R login() {
        String token = JwtUtils.genJwtToken("java1234");
        return R.ok().put("token", token);
    }

}
